#pragma once

#include <iostream>
#include <unistd.h>

using namespace std;

#define CMD_CODE_MAX 3
 
typedef void(*work_t)(int);
typedef void(*task_t)();

void Reading()
{
    cout << "Reading !!" << '\n';
}

void Writing()
{
    cout << "Writing !!" << '\n';
}

void Speaking()
{
    cout << "Speaking !!" << '\n';
}

task_t tasks[3] = {Reading, Writing, Speaking};     // task list (func_ptr array)

uint32_t NextTask()     // random calls which task
{
    return rand() % 3;
}

void worker(int rfd)   // acquire task from pipefd[0]
{
    while(true)
    {
        uint32_t command_code = 0;
        // manual-> ssize_t read(int fd, void *buf, size_t count);
        ssize_t n = read(rfd, &command_code, sizeof(command_code));
        if(n == sizeof(command_code))
        {
            if(command_code >= CMD_CODE_MAX)
            {
                continue;
            }
            tasks[command_code]();      // perform specified task
        }
        cout << "My work pid: "<< getpid() << endl;
        sleep(1);
    }
}
